package com.intelligent.controller;

import com.intelligent.service.CeoDashboardService;
import com.intelligent.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * CEO看板控制器
 * 提供CEO看板所需的各种数据统计API
 */
@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private CeoDashboardService ceoDashboardService;

    /**
     * 获取CEO看板核心统计数据
     * 包括销售收入、订单数量、客户数量等核心指标
     */
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getStatistics() {
        try {
            Map<String, Object> statistics = ceoDashboardService.getCoreStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取销售趋势数据
     * 返回最近指定月份的销售趋势
     */
    @GetMapping("/sales-trend")
    public Result<Map<String, Object>> getSalesTrend(@RequestParam(defaultValue = "12") int months) {
        try {
            Map<String, Object> trendData = ceoDashboardService.getSalesTrend(months);
            return Result.success(trendData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取销售趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取采购商品金额趋势
     * 返回最近指定月份的采购趋势
     */
    @GetMapping("/procurement-trend")
    public Result<Map<String, Object>> getProcurementTrend(@RequestParam(defaultValue = "12") int months) {
        try {
            Map<String, Object> trendData = ceoDashboardService.getProcurementTrend(months);
            return Result.success(trendData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取采购趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取生产合格率趋势
     * 返回最近指定月份的生产合格率
     */
    @GetMapping("/production-quality-trend")
    public Result<Map<String, Object>> getProductionQualityTrend(@RequestParam(defaultValue = "12") int months) {
        try {
            Map<String, Object> trendData = ceoDashboardService.getProductionQualityTrend(months);
            return Result.success(trendData);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取生产合格率趋势数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取订单销售额分布数据
     * 返回不同类别订单的销售额分布
     */
    @GetMapping("/order-distribution")
    public Result<List<Map<String, Object>>> getOrderDistribution() {
        try {
            List<Map<String, Object>> distribution = ceoDashboardService.getOrderDistribution();
            return Result.success(distribution);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取订单分布数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取最新订单列表
     * 返回最新的订单信息，支持分页
     */
    @GetMapping("/latest-orders")
    public Result<Map<String, Object>> getLatestOrders(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit) {
        try {
            Map<String, Object> result = ceoDashboardService.getLatestOrdersWithPagination(page, limit);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取最新订单数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取最新订单简化列表（不含分页信息）
     * 仅返回数据数组，保持原有分页接口不变
     */
    @GetMapping("/latest-orders-simple")
    public Result<List<Map<String, Object>>> getLatestOrdersSimple(@RequestParam(defaultValue = "10") int limit) {
        try {
            List<Map<String, Object>> data = ceoDashboardService.getLatestOrdersSimple(limit);
            return Result.success(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取最新订单简化数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取库存预警信息
     * 返回库存不足的产品信息
     */
    @GetMapping("/inventory-alerts")
    public Result<Map<String, Object>> getInventoryAlerts() {
        try {
            Map<String, Object> alerts = ceoDashboardService.getInventoryAlerts();
            return Result.success(alerts);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取库存预警数据失败: " + e.getMessage());
        }
    }
}